// ...........................................................................................................
// (C) Copyright  1996/2008 Fuego Inc.  All Rights Reserved
// THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Fuego Inc.
// The copyright notice above does not evidence any actual or intended
// publication of such source code.
//
// Last changed on 2008-01-11 17:12:10 (-0300), by: machaval. $Revision$
// ...........................................................................................................

package com.webcamtracker.ui.actions;

import com.webcamtracker.ui.UITracker;

import javax.media.CaptureDeviceInfo;
import javax.media.Format;
import javax.media.format.VideoFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Vector;


public class StartWebcamAction
        implements Action {
    //~ Instance fields ......................................................................................

    private PropertyChangeListener listener;
    private UITracker manager;
    private boolean isStarted = false;

    //~ Constructors .........................................................................................

    public StartWebcamAction(UITracker manager) {
        this.manager = manager;
    }


    public boolean isEnabled() {
        return true;
    }

    public void setEnabled(boolean b) {
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        this.listener = listener;
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        this.listener = null;
    }

    public Object getValue(String key) {
        String result = null;

        if (key.equals(Action.NAME)) {
            if (!isStarted) {
                result = START_VIDEO;
            } else {
                result = STOP_VIDEO;
            }
        }

        return result;
    }

    public void putValue(String key, Object value) {
    }

    public void actionPerformed(ActionEvent e) {
        if (!isStarted) {

            final StartWebcamAction.PresentationCaptureDeviceInfo deviceInfo = selectWebCam();
            if (deviceInfo != null) {
                PresentationVideoFormat selected = selectFormat(deviceInfo.getInfo());
                if (selected != null) {
                    manager.getVideo().startWebCam(deviceInfo.getInfo(), selected.format);
                    isStarted = true;
                    listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, START_VIDEO,
                            STOP_VIDEO));
                }
            }
        } else {
            manager.getVideo().stopVideo();
            isStarted = false;
            listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, STOP_VIDEO,
                    START_VIDEO));
        }
    }

    private PresentationCaptureDeviceInfo selectWebCam() {
        final Vector captureDeviceInfo = manager.getVideo().getVideoManager().getWebCamDeviceInfo();
        java.util.List<PresentationCaptureDeviceInfo> uiFormats = new ArrayList<PresentationCaptureDeviceInfo>();
        for (Object o : captureDeviceInfo) {
            uiFormats.add(new PresentationCaptureDeviceInfo((CaptureDeviceInfo) o));
        }
        return (PresentationCaptureDeviceInfo) JOptionPane.showInputDialog(manager,
                "Select Video format",
                "Capture format selection",
                JOptionPane.INFORMATION_MESSAGE,
                null,        //  Icon icon,
                uiFormats.toArray(), // videoFormats,
                uiFormats.get(0));

    }

    private PresentationVideoFormat selectFormat(CaptureDeviceInfo captureDeviceInfo) {

        final Format[] formats = captureDeviceInfo.getFormats();
        java.util.List<PresentationVideoFormat> uiFormats = new ArrayList<PresentationVideoFormat>();
        for (Format format : formats) {
            if (format instanceof VideoFormat) {
                uiFormats.add(new PresentationVideoFormat((VideoFormat) format));
            }
        }
        return (PresentationVideoFormat) JOptionPane.showInputDialog(manager,
                "Select Video format",
                "Capture format selection",
                JOptionPane.INFORMATION_MESSAGE,
                null,        //  Icon icon,
                uiFormats.toArray(), // videoFormats,
                uiFormats.get(0));
    }

    //~ Static fields/initializers ...........................................................................

    private class PresentationVideoFormat {
        public VideoFormat format;

        public PresentationVideoFormat(VideoFormat format) {
            this.format = format;
        }

        public String toString() {
            Dimension dim = format.getSize();
            return (format.getEncoding() + " [ " + dim.width + " x " + dim.height + " ]");
        }
    }

    private class PresentationCaptureDeviceInfo {
        private CaptureDeviceInfo info;

        public PresentationCaptureDeviceInfo(CaptureDeviceInfo format) {
            this.info = format;
        }

        public CaptureDeviceInfo getInfo() {
            return info;
        }

        public String toString() {
            return info.getName();
        }
    }

    public static final String START_VIDEO = "Start WebCam";
    public static final String STOP_VIDEO = "Stop WebCam";
}